﻿namespace DymanicArray
{
    using System;
    using System.Collections;

    /// <summary>
    /// Class for dynamic array
    /// </summary>
    public class MyDynList : ISuperList
    {
        private class DynArrayIterator : IEnumerator
        {
            public DynArrayIterator(DynArray list)
            {
                m_DynArray = list;
            }

            public bool MoveNext()
            {
                m_position = (m_callTimes % 2 == 0) ? m_DynArray.Count - m_callTimes / 2 - 1 : m_callTimes / 2;
                m_callTimes++;
                return (m_callTimes <= m_DynArray.Count);
            }

            public void Reset()
            {
                m_callTimes = 0;
                m_position = 0;
            }

            object IEnumerator.Current
            {
                get
                {
                    try
                    {
                        return m_DynArray[m_position];
                    }
                    catch (IndexOutOfRangeException)
                    {
                        throw new IndexOutOfRangeException();
                    }
                }
            }

            #region Private Fields

            private DynArray m_DynArray;
            private int m_callTimes = 0;
            private int m_position = 0;

            #endregion

        }

        #region Constructors

        /// <summary>
        /// Initialize new dynamic mass
        /// </summary>
        public MyDynList()
        {
            m_DynArray = new DynArray();
        }

        #endregion

        #region ISuperList Methods

        /// <summary>
        /// Get amount of elements in mass
        /// </summary>
        public int Count { get { return m_DynArray.Count; } }

        /// <summary>
        /// Add element at the end
        /// </summary>
        /// <param name="value">Element value</param>
        public void Add(int value)
        {
            m_DynArray.Add(value);
        }

        /// <summary>
        /// Get list item by index
        /// </summary>
        /// <param name="index">Index value</param>
        /// <returns>Item value</returns>
        public int GetItem(int index)
        {
            return m_DynArray[index];
        }

        /// <summary>
        /// Clears list
        /// </summary>
        public void Clear()
        {
            m_DynArray.Clear();
        }

        public bool Compare(int[] comp)
        {
            if (comp == null)
                throw new ArgumentNullException();

            if (comp.Length != this.Count)
                return false;

            for (int i = 0; i < this.Count; i++)
            {
                if (comp[i] != GetItem(i))
                    return false;
            }

            return true;
        }

        #endregion

        #region IEnumerable implementation

        public IEnumerator GetEnumerator()
        {
            return (IEnumerator)new DynArrayIterator(this.m_DynArray);
        }

        #endregion

        #region Private Fields

        private DynArray m_DynArray;

        #endregion      

    }
}
